
/**
 * 配置代理地址到proxy.config.js
 * 使用在route中使用app.use('/xxx', getProxyUrl());
 * url：如果带有固定的代理地址,没有则默认代理地址
 */ 


const { createProxyMiddleware } = require('http-proxy-middleware')
const proxyConfig = require('./proxy.config')
const proxyList = Object.keys(proxyConfig.proxy)

const proxy = {
    getProxyUrl(url) {
        if (!url) {
            return proxyList.map(item => {
                return createProxyMiddleware(item, proxyConfig.proxy[item])
            })
        } else {
            const options = {
                target: url,
                changeOrigin: true,
            }
            return createProxyMiddleware(options)
        }
    }
}

module.exports = proxy